package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.CustomerStatementSnapshotDao;
import com.xbongbong.saas.domain.entity.CustomerStatementSnapshotEntity;
import com.xbongbong.saas.enums.CustomerStatementSnapshotEnum;
import com.xbongbong.saas.model.CustomerStatementSnapshotModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @author: shian
 * @date : 2020/12/30
 */
@Service("customerStatementSnapshotModel")
public class CustomerStatementSnapshotModelImpl implements CustomerStatementSnapshotModel {
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private CustomerStatementSnapshotDao customerStatementSnapshotDao;

    @Override
    public Integer insert(CustomerStatementSnapshotEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer success = customerStatementSnapshotDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CustomerStatementSnapshotEntity> list) throws XbbException {
        customerStatementSnapshotDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaasFormDataEntity item : list) {
            InsertDTO insertDTO = paasEsModel.getSaveDTO(item, IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT);
            insertList.add(insertDTO);
        }
        paasEsModel.insertBatch(insertList);
    }

    @Override
    public List<CustomerStatementSnapshotEntity> findEntitys(Map<String, Object> param) {
        return customerStatementSnapshotDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return customerStatementSnapshotDao.getEntitiesCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer update(CustomerStatementSnapshotEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        Integer success = customerStatementSnapshotDao.update(entity);
        if (Objects.equals(1, success)) {
            // 更新es
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public Integer updateBatchAbnormal(List<CustomerStatementSnapshotEntity> list, String corpid) throws XbbException {
        if(CollectionsUtil.isEmpty(list)){
            return 0;
        }
        Integer integer = customerStatementSnapshotDao.updateBatchAbnormal(list, corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (CustomerStatementSnapshotEntity entity : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT.getType() + "_" + entity.getCorpid() + "_" + entity.getId());
            //对账单id
            Long dataId = entity.getData().getLong(CustomerStatementSnapshotEnum.SUMMARY_ID.getAttr());
            insertDTO.setParent(entity.getCorpid() + "_" + dataId);
            entity.setDataId(entity.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(entity, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT);
            insertDTOList.add(insertDTO);
        }
        //默认不抛出
        paasEsModel.updateBatch(insertDTOList, WriteRequest.RefreshPolicy.WAIT_UNTIL, false);
        return integer;
    }

    @Override
    public CustomerStatementSnapshotEntity getByKey(Long key, String corpid) {
        return customerStatementSnapshotDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> snapshotIds, String corpid, List<CustomerStatementSnapshotEntity> list) throws XbbException {
        Integer integer = customerStatementSnapshotDao.deleteBatch(snapshotIds, corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (CustomerStatementSnapshotEntity entity : list){
            long parentId = entity.getData().getLongValue(CustomerStatementSnapshotEnum.SUMMARY_ID.getAttr());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + parentId);
            Map<String, Object> sourceMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            sourceMap.put(ParameterConstant.DEL, DelEnum.DELETE.getDel());
            sourceMap.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
            insertDTO.setSource(sourceMap);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CUSTOMER_STATEMENT_SNAPSHOT);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
        return integer;
    }

    @Override
    public void physicalDeleteBatch(List<Long> snapshotIds, String corpid) throws XbbException {
        customerStatementSnapshotDao.physicalDeleteBatch(snapshotIds, corpid);
    }
}
